2
2
.
.
2
2
.
.
1
1
L
L
i
i
t
t
e
e
r
r
a
a
l
l
s
s
I
I
n
n
f
f
o
o
Literal is string representation of both data type and data value: 10, "John", ["AUDI", "FIAT"]
Literal notations are different ways of constructing literals that represent the same data type: 10, +10, 012 or 0xA.
Literal types
LITERAL
EXAMPLE
DESCRIPTION
Nil
null
Represents null data type with value
Boolean
true
Represents boolean data type and value
Integer
10
Represents integer data type and value
Float
0.5678
Represents floating point data type and value
String
"text"
Represents string data type and value
Array
["AUDI", "FIAT"]
Represents string data type and value
N
N
i
i
l
l
Nil literal represents Nil Data Type and it is written by using case sensitive nil.
var age : Int? = nil
B
B
o
o
o
o
l
l
e
e
a
a
n
n
Boolean literal represents Boolean Data Type and value and it is written by using case sensitive true or false.
var access1 = true
var access2 : Bool = false
I
I
n
n
t
t
e
e
g
g
e
e
r
r
Integer literal can represent Signed and Unsigned Integer Data Types and supports multiple notations.
var age = -41 //Decimal notation (base 10).
age = -0b101001 //Binary notation (base 2 ).
age = -0o51 //Octal notation (base 8 ).
age = -0x29 //Hexadecimal notation (base 16).
F
F
l
l
o
o
a
a
t
t
Float literal can represents Float and Double Data Types and value and supports two notations.
var temp = 123.045 //Normal notation.
temp = 1.23045E+2 //Scientific notation.
S
S
t
t
r
r
i
i
n
n
g
g
String literal represents String Data Type and is written by enclosing sequence of characters inside double quotes.
var name = "John" //"John" is string literal.
var text:String = "First line \nSecond \t line." //Escaping characters to display multiple lines and tab.
Escape Sequences
SEQUENCE
DESCRIPTION
CODE
\n
linefeed
(LF or 0x0A (10) in ASCII)
\r
carriage return
(CR or 0x0D (13) in ASCII)
\t
horizontal tab
(HT or 0x09 (9) in ASCII)
\v
vertical tab
(VT or 0x0B (11) in ASCII)
\e
escape
(ESC or 0x1B (27) in ASCII)
\f
form feed
(FF or 0x0C (12) in ASCII)
\\
backslash
\$
dollar sign
\"
double-quote (only for Double Quotes and Heredoc)
\'
single-quote (only for Single Quotes and Nowdoc)
\[0-7]{1,3}
sequence of characters matching the regular expression
is a character in octal notation
\x[0-9A-Fa-f]{1,2}
sequence of characters matching the regular expression
is a character in hexadecimal notation
A
A
r
r
r
r
a
a
y
y
Array literal is written by enclosing sequence of other Literals inside square brackets.
Array
var ages = [24, 17, 22, 30] //[24, 17, 22, 30] is array literal
var cars = ["AUDI", "FIAT"] //["AUDI", "FIAT"] is array literal